package com.ac.scum.log.task.face.log.counsumer.stages.gamplay;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.ServerFtpLogExplosives;
import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.face.log.counsumer.stages.GameplayLogConsumer;
import com.ac.scum.log.task.mapper.log.ServerFTPLogExplosivesMapper;
import com.ac.scum.log.task.utils.map.GameMapZoneUtil;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
@AllArgsConstructor
public class ExplosivesConsumer {
    private final GameMapZoneUtil gameMapZoneUtil;
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final ServerFTPLogExplosivesMapper serverFTPLogExplosivesMapper;

    public void explosivesConsumer(List<LogConsumer.GameLogLine> lines, FTPQueueItem ftpQueueItem, GameplayLogConsumer gameplayLogConsumer) {
        try {
            Pattern pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogExplosives\\] (.*?). User: (.*?) \\((\\d+), (\\d+)\\). (Grenade|Ignitable explosive) name: (.*?). Location: X=([-\\d.]+) Y=([-\\d.]+) Z=([-\\d.]+)");
            Pattern pattern2 = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBaseBuilding\\] Item attached. User: (.+?) \\((\\d+), (\\d+)\\). Grenade name: (.*?)\\. Location: X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            List<ServerFtpLogExplosives> explosivesList = Lists.newArrayList();
            for (LogConsumer.GameLogLine line : lines) {
                Matcher matcher = pattern.matcher(line.getLineContext());
                Matcher matcher2 = pattern2.matcher(line.getLineContext());

                if (matcher.find()) {
                    ServerFtpLogExplosives explosives = new ServerFtpLogExplosives();
                    explosives.setId(snowflakeIdGenerator.nextId());
                    explosives.setFileId(ftpQueueItem.getFileId());
                    explosives.setServerId(ftpQueueItem.getServer().getId());
                    explosives.setLineStart(line.getStartByte());
                    explosives.setLineEnd(line.getEndByte());
                    explosives.setCreateTime(LocalDateTime.now());

                    explosives.setServerTime(gameplayLogConsumer.parseTimestamp(matcher.group(1), ftpQueueItem));
                    explosives.setAction(getExplosivesAction(matcher.group(2)));
                    explosives.setPlayerSteamId(Long.parseLong(matcher.group(5)));
                    Player player = gameplayLogConsumer.getOrInsertPlayerServer(explosives.getPlayerSteamId(), explosives.getServerId(), matcher.group(3));
                    explosives.setPlayerId(player.getId());
                    switch (matcher.group(7)) {
                        case "Grenade":
                            explosives.setTarget("手雷");
                            break;
                        case "Pipe Bomb":
                            explosives.setTarget("铁管炸弹");
                            break;
                        default:
                            explosives.setTarget(matcher.group(7));
                            break;
                    }
                    explosives.setLocation(matcher.group(8) + "," + matcher.group(9) + "," + matcher.group(10));
                    explosives.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(matcher.group(8)), Double.parseDouble(matcher.group(9))));
                    explosivesList.add(explosives);
                } else if (matcher2.find()) {
                    ServerFtpLogExplosives logExplosives = new ServerFtpLogExplosives();
                    logExplosives.setId(snowflakeIdGenerator.nextId());
                    logExplosives.setFileId(ftpQueueItem.getFileId());
                    logExplosives.setServerId(ftpQueueItem.getServer().getId());
                    logExplosives.setLineStart(line.getStartByte());
                    logExplosives.setLineEnd(line.getEndByte());
                    logExplosives.setCreateTime(LocalDateTime.now());

                    logExplosives.setAction(3);
                    logExplosives.setServerTime(gameplayLogConsumer.parseTimestamp(matcher2.group(1), ftpQueueItem));
                    logExplosives.setPlayerSteamId(Long.parseLong(matcher2.group(4)));
                    Player player = gameplayLogConsumer.getOrInsertPlayerServer(logExplosives.getPlayerSteamId(), logExplosives.getServerId(), matcher2.group(2));
                    logExplosives.setPlayerId(player.getId());
                    logExplosives.setTarget(matcher2.group(5));
                    logExplosives.setLocation(matcher2.group(6) + "," + matcher2.group(7) + "," + matcher2.group(8));
                    logExplosives.setLocationZone(gameMapZoneUtil.getZone(Double.parseDouble(matcher2.group(6)), Double.parseDouble(matcher2.group(7))));
                    explosivesList.add(logExplosives);
                } else {
                    gameplayLogConsumer.commitDiscardLog(ftpQueueItem, line, "explosives no match");
                }
            }
            explosivesList.forEach(serverFTPLogExplosivesMapper::insert);
        } catch (Exception e) {
            log.error("爆炸物日志异常", e);
        }
    }


    public Integer getExplosivesAction(String action) {
        return switch (action) {
            case "Crafted" -> 1;
            case "Pin pulled", "Detonated", "Ignited" -> 2;
            default -> 9;
        };

    }
}
